package com.gugee.ins.data.blogger.statistics.core;

import com.gugee.ins.common.constant.InsBloggerMsgTypeCodeEnum;
import com.gugee.ins.common.dal.service.product.InsProductDao;
import com.gugee.ins.common.dal.service.statistics.InsStProductBaseDao;
import com.gugee.ins.common.model.es.product.InsProductEsMapping;
import com.gugee.ins.common.model.mysql.product.InsProduct;
import com.gugee.ins.common.model.mysql.statistics.InsStProductBase;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * 商品统计
 * @author Huangxin
 */
@Service
public class StProductBaseService extends StBaseService{

    /**
     *
     */
    private static final String THREAD_POOL_NAME = "ProductBase-Thread";

    /**
     *
     */
    @Autowired
    private InsStProductBaseDao insStProductBaseDao;

    @Autowired
    private InsProductDao insProductDao;

    /**
     *
     */
    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(bloggerStatisticsAppConfig.getInsProductBaseSt(), THREAD_POOL_NAME);
        scheduleTemplate.attach(THREAD_POOL_NAME, forkJoinPool);
        workName = "ProductBase";
    }

    /**
     *
     * @param insBloggerMessage
     */
    @Override
    protected void statistics(InsBloggerMessage insBloggerMessage) {
        // 当前时间戳
        long start = System.currentTimeMillis() / 1000;
        long productId = insBloggerMessage.getUid();
        int typeCode = insBloggerMessage.getTypeCode();
        // 时间区间
        long timeRang = start - bloggerStatisticsAppConfig.getStProductIntervalHours() * 3600;
        long tagProductMediaTime;
        int dateCode;
        if(typeCode == InsBloggerMsgTypeCodeEnum.ST_PRODUCT_WEEK.getCode()){
            tagProductMediaTime = start - 7 * 24 * 3600;
            dateCode = 1;
        }else{
            tagProductMediaTime = start - bloggerStatisticsAppConfig.getStProductLastTagMediaDay() * 24 * 3600;
            dateCode = 0;
        }
        // 查找商品信息
        InsProduct insProduct = insProductDao.findByProductId(productId);
        if(insProduct == null){
            return;
        }

        // 查询数据库
        InsStProductBase insStProductBase = insStProductBaseDao.findByProductIdAndType(productId,dateCode);
        // 判断是否最近更新过，更新过就不再更新
        if(checkUpdate(insStProductBase,timeRang)){
            return;
        }
        boolean isUpdate = true;
        // 没有的话就初始化
        if(insStProductBase == null){
            insStProductBase = new InsStProductBase();
            insStProductBase.setCreateTime(start);
            insStProductBase.setDateType(dateCode);
            insStProductBase.setProductId(productId);
            isUpdate = false;
        }
        insStProductBase.setOverallCategoryName(insProduct.getOverallCategoryName());
        insStProductBase.setLastMediaTagTime(insProduct.getLastMediaTagTime());
        InsStProductBase stResult = insStProductBaseDao.findStResultByProductId(productId,tagProductMediaTime);
        insStProductBase.setInfluencerCount(stResult.getInfluencerCount());
        insStProductBase.setMediaCount(stResult.getMediaCount());
        insStProductBase.setTec(stResult.getTec());
        insStProductBase.setLastStTime(start);
        // 保存
        if(isUpdate){
            insStProductBaseDao.updateById(insStProductBase);
        }else{
            insStProductBaseDao.save(insStProductBase);
        }

        if(dateCode == 0){
            productToEs(insStProductBase);
        }
    }

    /**
     * 判断是否最近更新过，更新过就不再更新
     * @param insStProductBase
     * @param timeRang
     * @return
     */
    public boolean checkUpdate(InsStProductBase insStProductBase,long timeRang){
        if(insStProductBase == null || insStProductBase.getLastStTime() < timeRang){
            return false;
        }
        return true;
    }

    /**
     * 统计信息更新到ES
     * @param insStProductBase
     */
    public void productToEs(InsStProductBase insStProductBase){
        InsProduct insProduct = insProductDao.findByProductId(insStProductBase.getProductId());

        if(null != insProduct){
            InsProductEsMapping insProductEsMapping = new InsProductEsMapping();
            insProductEsMapping.setId(insProduct.getId());
            insProductEsMapping.setInfluencerCount(insStProductBase.getInfluencerCount());
            insProductEsMapping.setMediaCount(insStProductBase.getMediaCount());
            insProductEsMapping.setTec(insStProductBase.getTec());
            insProductEsMapping.setLastStTime(insStProductBase.getLastStTime());
            RestClientHelper.upsert(bloggerStatisticsAppConfig.getProductIndex(),insProductEsMapping);
        }
    }
}
